package com.oceansoft.mobile.econsole.modules.weixin.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.oceansoft.mobile.econsole.common.util.HttpClientUtil;
import com.oceansoft.mobile.econsole.modules.base.entity.Result;
import com.oceansoft.mobile.econsole.modules.weixin.entity.TcAdvice;
import com.oceansoft.mobile.econsole.modules.weixin.entity.TcDriverInfo;
import com.oceansoft.mobile.econsole.modules.weixin.entity.WxMember;
import com.oceansoft.mobile.econsole.modules.weixin.persistence.ITcAdviceMapper;
import com.oceansoft.mobile.econsole.modules.weixin.persistence.ITcDriverInfoMapper;
import com.oceansoft.mobile.econsole.modules.weixin.persistence.IWxMemberMapper;
import com.oceansoft.mobile.econsole.modules.weixin.service.IWxMemberService;
import com.oceansoft.mobile.econsole.task.sms.SMSUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class WxMemberServiceImpl implements IWxMemberService {
	@Resource
	private IWxMemberMapper iWxMemberMapper;
	@Resource
	private ITcDriverInfoMapper infoMapper;
	@Resource
	private ITcAdviceMapper adviceMapper;

	@Override
	public Result saveWxMember(WxMember wxMember) {
		int rt = iWxMemberMapper.insert(wxMember);
		if (rt > 0)
			return new Result(Result.SUCC, wxMember, "微信用户保存成功");
		return new Result(Result.FAILURE, "微信用户保存失败");
	}

	@Override
	public Result loadWxMemberById(Integer id) {
		WxMember member = iWxMemberMapper.selectByPrimaryKey(id);
		if (null != member)
			return new Result(member);
		return new Result(Result.FAILURE, "微信用户数据不存在");
	}

	@Override
	public List<WxMember> loadWxMemberByOpenId(String openid) {
		return iWxMemberMapper.selectByOpenId(openid);
	}

	@Override
	public WxMember findByOpenId(String openid) {
		List<WxMember> list = iWxMemberMapper.selectByOpenId(openid);
		if (!CollectionUtils.isEmpty(list))
			return list.get(0);

		return null;
	}

	@Override
	public List<TcDriverInfo> loadCarByOpenId(String openid) {
		return infoMapper.selectCarInfoByOpenId(openid);

	}

	@Override
	public List<TcDriverInfo> loadDcnByOpenId(String openid) {
		return infoMapper.selectDcnInfoByOpenId(openid);
	}

	@Override
	public Result submit(TcAdvice advice) {
		int count = adviceMapper.insert(advice);
		if (count > 0) {
			return new Result(Result.SUCC, "建议提交成功！");
		} else {
			return new Result(Result.FAILURE, "建议提交失败！");
		}
	}

	@Override
	public Result binInfo(String cardId,String mobile, String smsCode, HttpSession session) {
		if (!(smsCode.trim().equals(session.getAttribute("smscode") + "")))
			return new Result(Result.FAILURE, "验证码验证失败");

		String openid = "";
		if (null != session.getAttribute("openid"))
			openid = session.getAttribute("openid") + "";

		if (StringUtils.isEmpty(openid))
			return new Result(Result.FAILURE, "微信账号获取异常");
		if (iWxMemberMapper.findMemberByOpenId(openid) != null&&iWxMemberMapper.findMemberByOpenId(openid).getCardId()!=null)
			return new Result(Result.FAILURE, "此微信号已绑定车辆及驾驶证信息");
		//绑定帐号信息
		WxMember record = new WxMember();
		record.setCardId(cardId);
		record.setMobile(mobile);
		record.setOpenId(openid);
		int count = iWxMemberMapper.updateByOpenId(record);
		if(count>0){
			session.setAttribute("smscode", null);
			return new Result(Result.SUCC, "绑定成功");
		}else{
			session.setAttribute("smscode", null);
			return new Result(Result.FAILURE, "绑定失败");
		}
		
	}

	@Override
	public Result sendMsgcode(String mobile, HttpSession session) {
		long _code = Math.round(Math.random() * 8999 + 1000);
		session.setAttribute("smscode", _code);
		//new SmsThread(mobile, _code + "").start();
		SMSUtil.sendSMS("",String.valueOf(_code),mobile);
		return new Result("短信发送成功");
	}

	@Override
	public List<TcDriverInfo> loadDriverByOpenId(String openId) {
		return infoMapper.selectByOpenid(openId);
	}

	@Override
	public Result bindDriveInfo(String cardId,String openid) {
		try {
			if(!CollectionUtils.isEmpty(infoMapper.selectCarInfoByOpenId(openid))){
				return new Result(Result.FAILURE, "您已绑定！");
			}
			
			JSONObject json = JSONObject.parseObject(HttpClientUtil.doGet("http://221.224.13.5:8190/econsole/api/bind/info/vehicle/" + cardId));
			String data = json.get("data").toString();
			JSONObject data1 = JSONObject.parseObject(data);
			String jsz = data1.get("JSZ").toString();
			String cl = data1.get("CL").toString();
			JSONArray jszArray = JSONArray.parseArray(jsz);// 驾驶证信息
			JSONArray clArray = JSONArray.parseArray(cl);// 车辆信息

			if (jszArray.isEmpty() && clArray.isEmpty())
				return new Result(Result.FAILURE, "未查到车辆及驾驶证信息！");

			// 保存驾驶证
				if (!jszArray.isEmpty()) {
					for (int i = 0; i < jszArray.size(); i++) {
						JSONObject obj = jszArray.getJSONObject(i);
						TcDriverInfo driverInfo = new TcDriverInfo();
						driverInfo.setOpenId(openid);
						driverInfo.setDcn(obj.get("drivingLicense") + "");
						driverInfo.setArcId(obj.get("fn") + "");
						driverInfo.setCreateTime(new Date());
						infoMapper.insert(driverInfo);
					}
				}

			// 保存车辆信息
				if (!clArray.isEmpty()) {
					for (int i = 0; i < clArray.size(); i++) {
						JSONObject obj = clArray.getJSONObject(i);
						TcDriverInfo driverInfo = new TcDriverInfo();
						driverInfo.setOpenId(openid);
						driverInfo.setLpn("苏" + obj.getString("lpn"));
						driverInfo.setVin(obj.getString("vin"));
						driverInfo.setCreateTime(new Date());
						infoMapper.insert(driverInfo);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				return new Result(Result.FAILURE, "查询超时！");
			}
			return new Result(Result.SUCC, "绑定成功！");
		}

	/**
	 * 删除绑定
	 */
	@Override
	public Result delWxMemberById(String openid) {
		int count=iWxMemberMapper.delWxMemberById(openid);
		if(count>0){
			return new Result(Result.SUCC, String.format("删除成功", count));
		}
		return new Result(Result.SUCC, String.format("删除失败", count));
	}

	/**
	 * 根据openid删除记录
	 */
	@Override
	public Result delWxMemberByOpenId(String openId) {
		int count=iWxMemberMapper.delWxMemberByOpenId(openId);
		if(count>0){
			return new Result(Result.SUCC, String.format("删除成功", count));
		}
		return new Result(Result.SUCC, String.format("删除失败", count));
	}

	/**
	 * 根据openid删除驾车信息
	 */
	@Override
	public Result delWxDriverInfo(String openID) {
		int count=iWxMemberMapper.delWxDriverInfo(openID);
		if(count>0){
			return new Result(Result.SUCC, String.format("删除成功", count));
		}
		return new Result(Result.SUCC, String.format("删除失败", count));
	}
}